package com.th.supcom.pds.phamapply.server;

import java.util.ArrayList;
import java.util.List;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.dozer.Mapper;

import com.th.supcom.framework.organization.web.gwt.client.model.OrganizationModel;
import com.th.supcom.pds.common.enums.AvailableEnum;
import com.th.supcom.pds.common.enums.InpDispenseIndicator;
import com.th.supcom.pds.domain.pojo.InpPhamApplyDetailSummary;
import com.th.supcom.pds.domain.pojo.InpPhamApplyMaster;
import com.th.supcom.pds.domain.pojo.InpPhamDispenseDetail;
import com.th.supcom.pds.domain.pojo.InpPhamDispenseResult;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.domain.vo.InpPhamDispenseDetailVO;
import com.th.supcom.pds.internal.api.phamapply.InpPhamApplyDetailBasicService;
import com.th.supcom.pds.internal.api.phamapply.PhamDispenseService;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyDetailModel;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyDetailSummaryModel;
import com.th.supcom.pds.phamapply.client.model.InpPhamDispenseDetailModel;
import com.th.supcom.pds.phamapply.client.model.InpPhamDispenseResultModel;
import com.th.supcom.pds.phamapply.client.service.InpPhamDispenseDetailRpc;

//编写代码时释放此注释，（通过代码检查）
//import net.carefx.framework.util.ConvertUtils;
/**
 * InpPhamApplyMaster 控制层.
 * 
 * 住院摆药请领单.
 */
public class InpPhamDispenseDetailServiceController extends GwtController implements InpPhamDispenseDetailRpc
{
    private static final long serialVersionUID = 1L;
    private InpPhamApplyDetailBasicService m_inpPhamApplyDetailBasicService;
    private PhamDispenseService m_phamDispenseService;
    private Mapper m_mapper;

    /**
     * 默认构造函数.
     */
    public InpPhamDispenseDetailServiceController ()
    {

    }

    public void setInpPhamApplyDetailBasicService (InpPhamApplyDetailBasicService inpPhamApplyDetailBasicService)
    {

        m_inpPhamApplyDetailBasicService = inpPhamApplyDetailBasicService;
    }

    public void setMapper (Mapper mapper)
    {

        m_mapper = mapper;
    }

    public void setPhamDispenseService (PhamDispenseService phamDispenseService)
    {

        m_phamDispenseService = phamDispenseService;
    }

    @Override
    public List <InpPhamApplyDetailModel> getInpPhamApplyNurseUnit (String deptCode) throws UiException
    {
        InpPhamApplyDetailVO inpPhamApplyDetail = new InpPhamApplyDetailVO ();
        inpPhamApplyDetail.setApplyDispensary (deptCode);
        inpPhamApplyDetail.setDispenseIndicator (InpDispenseIndicator.Supplyed.getEnumCode ());
        List <InpPhamApplyDetailVO> list = m_inpPhamApplyDetailBasicService.findInpPhamApplyDetailListByCondition (inpPhamApplyDetail);
        // 将结果转成model
        List <InpPhamApplyDetailModel> result = new ArrayList <InpPhamApplyDetailModel> ();

        for (InpPhamApplyDetailVO detail : list)
        {
            result.add (m_mapper.map (detail, InpPhamApplyDetailModel.class));
        }

        return result;
    }

    @Override
    public List <InpPhamApplyDetailModel> getApplyDetailByApplyNoAndDispensary (String applyNo, String dispensary) throws UiException
    {
        List <InpPhamApplyDetailVO> list = m_phamDispenseService.obtainApplyDetailByApplyNoAndDispensary (applyNo, dispensary);
        // 将结果转成model
        List <InpPhamApplyDetailModel> result = new ArrayList <InpPhamApplyDetailModel> ();
        for (InpPhamApplyDetailVO detail : list)
        {
            result.add (m_mapper.map (detail, InpPhamApplyDetailModel.class));
        }
        return result;
    }

    @Override
    public void execPhamApplySheet (List <InpPhamApplyDetailModel> applyList, String operator, String dispensary) throws UiException
    {
        // 将model转成vo
        List <InpPhamApplyDetailVO> list = new ArrayList <InpPhamApplyDetailVO> ();
        for (InpPhamApplyDetailModel model : applyList)
        {
            list.add (m_mapper.map (model, InpPhamApplyDetailVO.class));
        }
        if (list.isEmpty ())
        {
            throw new BusinessException ("没有明细要执行！");
        }
        InpPhamApplyMaster master = new InpPhamApplyMaster ();
        master.setNurseUnit (applyList.get (0).getNurseUnit ());
        m_phamDispenseService.execPhamApplySheet (list, master, operator, dispensary);
    }

    @Override
    public List <InpPhamDispenseDetailModel> findInpPhamDispenseNurse (String dispensary) throws UiException
    {
        List <InpPhamDispenseDetail> list = m_phamDispenseService.findInpPhamDispenseNurse (dispensary);
        // 将结果转成model
        List <InpPhamDispenseDetailModel> result = new ArrayList <InpPhamDispenseDetailModel> ();
        for (InpPhamDispenseDetail detail : list)
        {
            result.add (m_mapper.map (detail, InpPhamDispenseDetailModel.class));
        }
        return result;
    }

    @Override
    public InpPhamDispenseResultModel getInpCheckDispenseDetail (String applyNo, String dispensary, String nurseUnit) throws UiException
    {
        InpPhamDispenseDetailVO inpPhamDispenseDetailVO = new InpPhamDispenseDetailVO ();
        inpPhamDispenseDetailVO.setApplyNo (applyNo);
        inpPhamDispenseDetailVO.setNurseUnit (nurseUnit);
        inpPhamDispenseDetailVO.setPerformDispensary (dispensary);
        InpPhamDispenseResult inpPhamDispenseResult = m_phamDispenseService.findInpPhamDispenseByExample (inpPhamDispenseDetailVO);
        // 将结果转成model
        List <InpPhamDispenseDetailModel> dispenseDetailModels = new ArrayList <InpPhamDispenseDetailModel> ();
        for (InpPhamDispenseDetailVO detail : inpPhamDispenseResult.getInpPhamDispenseDetailVOs ())
        {
            dispenseDetailModels.add (m_mapper.map (detail, InpPhamDispenseDetailModel.class));
        }
        List <InpPhamApplyDetailSummaryModel> summaryModels = new ArrayList <InpPhamApplyDetailSummaryModel> ();
        for (InpPhamApplyDetailSummary detail : inpPhamDispenseResult.getInpPhamApplyDetailSummaries ())
        {
            summaryModels.add (m_mapper.map (detail, InpPhamApplyDetailSummaryModel.class));
        }
        InpPhamDispenseResultModel inpPhamDispenseResultModel = new InpPhamDispenseResultModel ();
        inpPhamDispenseResultModel.setInpPhamApplyDetailSummaryModels (summaryModels);
        inpPhamDispenseResultModel.setInpPhamDispenseDetailModels (dispenseDetailModels);
        return inpPhamDispenseResultModel;
    }

    @Override
    public void verifyDispenseDetail (InpPhamDispenseResultModel inpPhamDispenseResultModel, String operator, String dispensary) throws UiException
    {
        List <InpPhamDispenseDetailVO> list = new ArrayList <InpPhamDispenseDetailVO> ();
        List <InpPhamApplyDetailSummary> summaries = new ArrayList <InpPhamApplyDetailSummary> ();
        InpPhamDispenseResult inpPhamDispenseResult = new InpPhamDispenseResult ();
        for (InpPhamDispenseDetailModel model : inpPhamDispenseResultModel.getInpPhamDispenseDetailModels ())
        {
            list.add (m_mapper.map (model, InpPhamDispenseDetailVO.class));
        }
        for (InpPhamApplyDetailSummaryModel inpPhamApplyDetailSummaryModel : inpPhamDispenseResultModel.getInpPhamApplyDetailSummaryModels ())
        {
            summaries.add (m_mapper.map (inpPhamApplyDetailSummaryModel, InpPhamApplyDetailSummary.class));
        }
        inpPhamDispenseResult.setInpPhamApplyDetailSummaries (summaries);
        inpPhamDispenseResult.setInpPhamDispenseDetailVOs (list);
        if (list.isEmpty ())
        {
            throw new BusinessException ("没有摆药明细要核对！");
        }
        m_phamDispenseService.verifyDispenseDetail (inpPhamDispenseResult, operator, dispensary);
    }

    @Override
    public List <OrganizationModel> findAvailableDispensary (String phamStdCode)
    {
        List <OrganizationModel> result = new ArrayList <OrganizationModel> ();
        InpPhamApplyDetailSummary summary = new InpPhamApplyDetailSummary ();
        summary.setPhamStdCode (phamStdCode);
        summary.setApplySupplyFlag (AvailableEnum.YES.getEnumCode ());

        List <InpPhamApplyDetailSummary> list = m_phamDispenseService.findAvailableDispensary (summary);
        if (null == list)
        {
            return result;
        }

        for (InpPhamApplyDetailSummary inpPhamApplyDetailSummary : list)
        {
            OrganizationModel model = new OrganizationModel ();
            model.setName (inpPhamApplyDetailSummary.getDispensaryName ());
            model.setCode (inpPhamApplyDetailSummary.getDispensary ());
            result.add (model);
        }
        return result;
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    @Override
    public void cancelApplyMaster (String applyNo, String dispensary) throws UiException
    {
        InpPhamApplyMaster master = new InpPhamApplyMaster ();
        master.setApplyNo (applyNo);
        master.setNurseUnit (dispensary);
        master.setIsCancel (1);
        m_phamDispenseService.cancelPhamApplySheet (master);
    }

    @Override
    public List <InpPhamApplyDetailModel> getInpPhamCancelNurseUnit (InpPhamApplyDetailModel model) throws UiException
    {
        InpPhamApplyDetailVO inpPhamApplyDetail = new InpPhamApplyDetailVO ();
        inpPhamApplyDetail.setNurseUnit (model.getNurseUnit ());
        inpPhamApplyDetail.setBeginDateOfDay (model.getBeginDateOfDay ());
        inpPhamApplyDetail.setEndDateOfDay (model.getEndDateOfDay ());
        List <InpPhamApplyDetailVO> list = m_inpPhamApplyDetailBasicService.findInpPhamApplyCancelListByCondition (inpPhamApplyDetail);
        // 将结果转成model
        List <InpPhamApplyDetailModel> result = new ArrayList <InpPhamApplyDetailModel> ();

        for (InpPhamApplyDetailVO detail : list)
        {
            result.add (m_mapper.map (detail, InpPhamApplyDetailModel.class));
        }

        return result;

    }

    @Override
    public List <InpPhamDispenseDetailModel> getInpCheckDispenseDetail (InpPhamDispenseDetailModel inpPhamDispenseDetailModel) throws UiException
    {
        InpPhamDispenseDetail inpPhamDispenseDetail = ConvertUtils.convert (InpPhamDispenseDetail.class, inpPhamDispenseDetailModel);
        List <InpPhamDispenseDetailVO> details = m_phamDispenseService.findInpPhamDispenseDetail (inpPhamDispenseDetail);
        // 将结果转成model
        List <InpPhamDispenseDetailModel> result = new ArrayList <InpPhamDispenseDetailModel> ();
        for (InpPhamDispenseDetail detail : details)
        {
            result.add (m_mapper.map (detail, InpPhamDispenseDetailModel.class));
        }
        return result;

    }
}